<?php
class NewsBlockWidget extends CWidget
{
	public $znc_id = 0;
	public $limit = 10;
	public $offset = 0;
	public $showDate = false;
	public $dateFormat = 'm/d';
	public $order = 'zne_commend DESC, zne_id DESC';
	public $shortTitle = false;
	public $commend = '';
	public $ulclass = '';
	public $banner = 0;
	public $cache = YII_DEBUG;
	
	public function run()
	{
		 $cacheKey = 'news-block-widget-'.$this->znc_id.$this->limit.$this->offset
		.$this->showDate.$this->dateFormat.$this->order
		.$this->shortTitle.$this->ulclass;
			
		 $html = !$this->cache ? Yii::app()->cache->get($cacheKey) : false;
		
		 if($html === false)
		 {
			$criteria = new CDbCriteria;
			$criteria->compare('zne_status', 2);
			$criteria->limit = $this->limit;
			$criteria->offset = $this->offset;
			$criteria->order = $this->order;
			
			//if($this->city_id) $criteria->compare('city_id', $this->city_id);
			
			if($this->znc_id) 
			{
				if(is_numeric($this->znc_id))
				{
					$category = NewsCat::model()->findByPk($this->znc_id);
					$criteria->compare('znc_id', $this->znc_id);
				}
				elseif(is_array($this->znc_id))
					$criteria->addInCondition('znc_id', $this->znc_id);
			}
						
			if($this->commend) $criteria->compare('zne_commend', $this->commend);
			
			$news = News::model()->findAll($criteria);
			
			
			//echo array_shift($news);
			$html = "<ul class=\"{$this->ulclass}\">\r\n";
			foreach($news as $index=>$val)
			{
				switch ($val->newscat->znc_patentid)
				{
				case 7:
				  $url = 'home/news/album_detail/id/'.$val->zne_id;
				  break;
				case 6:
				  $url = '/home/news/video_detail/id/'.$val->zne_id;
				  break;
				default:
					$url = '/home/news/detail/id/'.$val->zne_id;
				}				
				
				$title = $val->zne_short;
				if($index  == 0 && $this->banner == 1)
				{
					$html .= '<div class="img_news c_f">
								<div class="c_img c_img100">'.CHtml::link(Tool::thumb($val->zne_thumb,100,100, $title), $url, array('target'=>'_blank')).'</div>
								<h3 class="fs_14 ff_yh">'.CHtml::link($title, $url, array('target'=>'_blank', 'title'=>$title)).'</h3>
								<p class="c_999 ff_s">'.$val->zne_desc.'</p>
							</div>';
				}
				if($index  == 0 && $this->banner == 2)
				{
					$html .='<li class="click_img c_f">
                            <div class="img f_l">'.CHtml::link(Tool::thumb($val->zne_thumb,60,60, $title), $url, array('target'=>'_blank')).'</div>
                            <h3 class="txt">'.CHtml::link($title, $url, array('target'=>'_blank', 'title'=>$title, 'class'=>'c_blue3')).'</h3>
                        </li>
					';
				}
				else
				{
					$html .= '<li>'
					.CHtml::link(
					$title, 
					$url, 
					array('target'=>'_blank', 'title'=>$title)
					).'<span class="time f_r c_999">'.
					($this->showDate ? date($this->dateFormat, $val->inputtime):'')
					."</span></li>\r\n";
				}
			}
			
			$html .= '</ul>';
			
			if(!$this->cache) Yii::app()->cache->set($cacheKey, $html, 15*60);
		}
		echo $html;
	}
}
